/*
 * File:    SplashWindow.java
 * Author:  Ryan J. Bovorasmy
 * Date:    2008-03-??
 * Desc:    Used to create a Splash Window.
 */
package protovitae.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;

/**
 * Used to create a Splash Window.
 * @author Ryan J. Bovorasmy
 */
public class SplashWindow extends JWindow {

    public SplashWindow(Frame parent, File file, int waitTime) {
        super(parent);
        JLabel l = new JLabel(new ImageIcon(file.getAbsolutePath()));
        getContentPane().add(l, BorderLayout.CENTER);
        pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = l.getPreferredSize();
        setLocation(screenSize.width / 2 - (labelSize.width / 2), screenSize.height / 2 - (labelSize.height / 2));
        final int pause = waitTime;
        final Runnable closerRunner = new Runnable() {
            public void run() {
                setVisible(false);
                dispose();
            }
        };
        Runnable waitRunner = new Runnable() {
            public void run() {
                try {
                    Thread.sleep(pause);
                    SwingUtilities.invokeAndWait(closerRunner);
                } catch (Exception e) {
                    System.err.println(e.toString());
                }
            }
        };
        this.setAlwaysOnTop(true);
        setVisible(true);
        Thread splashThread = new Thread(waitRunner, "SplashThread");
        splashThread.start();
    }
}
